import tkinter as tk
from array import array
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import pandas as pd

from data import DataSet, DataUtils
from train import TrainUtils
from visual import VisualUtils


class PredictView:
	def __init__(self, parent_window):
		self.window = tk.Tk()
		self.window.title("主页")
		# 窗口大小
		self.window.geometry("860x520")
		# 创建树
		self.tree = None
		self.data_set = None
		self.creat_tree()
		# for i in range(0, 9):
		Label(self.window, text='性别(-1, 0, 1)', font=("Verdana", 12)).grid(
			row=3, column=0, pady=3)
		Label(self.window, text='长度(0.055-0.915)', font=("Verdana", 12)).grid(
			row=3, column=1, pady=3)
		Label(self.window, text='直径(0.035-0.750)', font=("Verdana", 12)).grid(
			row=3, column=2, pady=3)
		Label(self.window, text='高度(0.001-1.230)', font=("Verdana", 12)).grid(
			row=3, column=3, pady=3)
		Label(self.window, text='整体重量(0.001-2.925)', font=("Verdana", 12)).grid(
			row=4, column=0, pady=3)
		Label(self.window, text='肉重量(0.001-1.588)', font=("Verdana", 12)).grid(
			row=4, column=1, pady=3)
		Label(self.window, text='内脏重量(0.0001-0.860)', font=("Verdana", 12)).grid(
			row=4, column=2, pady=3)
		Label(self.window, text='壳重量(0.0001-1.105)', font=("Verdana", 12)).grid(
			row=4, column=3, pady=3)
		Button(self.window, text='刷新测试数据', width='20', command=self.create_test_data, font=("Verdana", 12)).grid(
			row=5, column=0, padx=5, pady=10)
		Button(self.window, text='线性回归预测', width='20', command=self.linear_predict, font=("Verdana", 12)).grid(
			row=5, column=1, padx=5, pady=10)
		(Button(self.window, text='决策树预测', width='20', command=self.tree_predict, font=("Verdana", 12)).grid(
			row=5, column=2, padx=5, pady=10))

	def create_test_data(self):
		x = self.tree.get_children()
		for item in x:
			self.tree.delete(item)  # 删除
		self.insert_data()

	def linear_predict(self):
		data = pd.DataFrame(columns=DataUtils.names[:8])
		ds = self.data_set
		for col in range(0, DataUtils.test_size):
			data.loc[col] = [ds[col].sex, ds[col].length, ds[col].diameter, ds[col].height, ds[col].whole_weight, ds[col].shucked_weight, ds[col].viscera_weight, ds[col].shell_weight]
		TrainUtils.linear_model_predict(data, DataUtils.test_y)

	def tree_predict(self):
		data = pd.DataFrame(columns=DataUtils.names[:8])
		ds = self.data_set
		for col in range(0, DataUtils.test_size):
			data.loc[col] = [ds[col].sex, ds[col].length, ds[col].diameter, ds[col].height, ds[col].whole_weight, ds[col].shucked_weight, ds[col].viscera_weight, ds[col].shell_weight]
		TrainUtils.tree_model_predict(data, DataUtils.test_y)

	def creat_tree(self):
		columns = ['性别', '长度', '直径', '高度', '整体重量', '肉重量', '内脏重量', '壳重量']
		self.tree = ttk.Treeview(self.window, show="headings", columns=columns)
		vbar = ttk.Scrollbar(self.window, orient=VERTICAL, command=self.tree.yview)
		# 定义树形结构与滚动条
		self.tree.configure(yscrollcommand=vbar.set, height=15)
		ttk.Style().configure("Treeview.Heading", font=("宋体", 18))
		ttk.Style().configure("Treeview", font=("宋体", 18))
		# 调用方法获取表格内容插入
		self.tree.grid(row=0, rowspan=3, column=0, columnspan=8, sticky=NSEW, padx=5, pady=5)
		vbar.grid(row=0, column=8, sticky=NS)
		for col in columns:
			self.tree.column(column=col, width=80, anchor='center')
			self.tree.heading(col, text=col)
		self.insert_data()

	def insert_data(self):
		data = DataUtils.create_dataset()
		self.data_set = data
		for row in range(DataUtils.test_size):
			self.tree.insert(index=row, parent='',  values=(data[row].sex, data[row].length, data[row].diameter,
				data[row].height, data[row].whole_weight, data[row].shucked_weight, data[row].viscera_weight, data[row].shell_weight))
